<!--
 * @Author: your name
 * @Date: 2021-09-18 01:57:51
 * @LastEditTime: 2021-09-23 15:36:46
 * @LastEditors: Please set LastEditors
 * @Description: 下拉菜单
 * @FilePath: \uniapp-cil\src\components\x-dropdown-item\x-dropdown-item.vue
-->
<template>
  <view
    class="bg_w"
    v-if="active"
    @touchmove.stop.prevent="() => {}"
    @tap.stop.prevent="() => {}"
  >
    <scroll-view
      :scroll-y="options.length > 6"
      :style="{ height: options.length > 6 ? '600rpx' : 'auto' }"
    >
      <view
        v-for="(item, index) in options"
        :key="index"
        @click="activeItem(item)"
      >
        <view
          class="p_row_25 p_col_30 b_b_s_1 menu_item u-flex u-row-between"
          :class="[`f_w_${fontWeight}`, { active_mask: item.value == value }]"
          :style="{ color: item.value == value ? activeColor : inactiveColor }"
        >
          <text>{{ item.label }}</text>
          <view
            ><u-icon
              v-if="value == item.value"
              name="checkbox-mark"
              :color="activeColor"
              size="32"
            ></u-icon
          ></view>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
export default {
  name: "x-dropdown-item",
  props: {
    fontWeight: {
      type: Number,
      default: 500,
    },
    // 当前选中项的value值
    value: {
      type: [Number, String, Array],
      default: "",
    },
    title:{
      type:String,
      required:true
    },
    disabled:{
      type:Boolean,
      default:false
    },
    icon:{
      type:String,
      default:""
    },
    customIcon:{
        type:String,
        default:'',
    },
    options: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  data() {
    return {
      active: false, // 当前项是否处于展开状态
      activeColor: "#2979ff", // 激活时左边文字和右边对勾图标的颜色
      inactiveColor: "#606266", // 未激活时左边文字和右边对勾图标的颜色
    };
  },
  created() {
    // 父组件实例
    this.parent = {};
  },
  mounted() {
    this.init();
  },

  computed: {
  	// 监听props是否发生了变化，有些值需要传递给父组件u-dropdown，无法双向绑定
  	propsChange() {
  		return `${this.title}-${this.disabled}-${this.icon}-${this.customIcon}`;
  	}
  },
  watch: {
  	propsChange(n) {
  		// 当值变化时，通知父组件重新初始化，让父组件执行每个子组件的init()方法
  		// 将所有子组件数据重新整理一遍
  		if (this.parent) this.parent.init();
  	}
  },
  methods: {
    init() {
      // 获取父组件x-dropdown
      let parent = this.$u.$parent.call(this, "x-dropdown");
      if (parent) {
        this.parent = parent;
        // 将子组件的激活颜色配置为父组件设置的激活和未激活时的颜色
        this.activeColor = parent.activeColor;
        this.inactiveColor = parent.inactiveColor;
        // 将本组件的this，放入到父组件的children数组中，让父组件可以操作本(子)组件的方法和属性
        // push进去前，显判断是否已经存在了本实例，因为在子组件内部数据变化时，会通过父组件重新初始化子组件
        let exist = parent.children.find((val) => {
          return this === val;
        });
        if (!exist) parent.children.push(this);
        if (parent.children.length == 1) this.active = true;
        // 父组件无法监听children的变化，故将子组件的title，传入父组件的menuList数组中
        parent.menuList.push({
        	title: this.title,
        	disabled: this.disabled,
            icon:this.icon,
            customIcon:this.customIcon,
        });
      }
    },
    activeItem(item) {
      // 修改通过v-model绑定的值
      this.$emit("input", item.value);
      // 通知父组件(x-dropdown)收起菜单
      this.parent.close();
      // 发出事件，抛出当前勾选项的value
      this.$emit("change", item);
    },
  },
};
</script>

<style lang="scss" scoped>
.active_mask {
  background-color: rgba(0, 0, 0, 0.1);
}
</style>